﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _01_委托
{
    class Program
    {
        delegate int delegateSetValues(int x, int y, int z);//参数和对象的方法参数要对应
        delegate int delegateSetValues_01(int x);
        delegate void del();

        static void Main(string[] args)
        {
            Sample01();
            Sample02();
            Sample03();
            Sample04();
            Sample05();
            Console.ReadKey();
        }

        static void Sample01()
        {
            Operator p = new Operator();
            delegateSetValues ds = new delegateSetValues(p.SetValues);
            delegateSetValues_01 ds_01 = new delegateSetValues_01(p.SetValues);
            Console.WriteLine(ds(11, 12, 12));
            Console.WriteLine(ds_01(10));
        }
        static void Sample02()
        {
            string mid = " 说超人挂了";
            //public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
            //第三个参数是传出，其他都是传入参数，详细看定义
            Func<string, string, string> delegateFunc = delegate(string param_01, string param_02)
            {
                return param_01 + param_02 + mid;

            };
            //通过参数的比较，我们可以看到，Func和Action的区别，
            //Func最后一个参数是返回值
            //Action没有返回值
            Action<string, string, string> delegateAction = delegate(string param_01, string param_02, string param_03)
            {
                mid = param_01 + param_02 + param_03 + mid;
            };

            //运行部分
            Console.WriteLine(delegateFunc("路人甲、", "路人乙"));
            delegateAction("路人甲、", "路人乙、", "路人丁");
            Console.WriteLine(mid);

        }
        static void Sample03()
        {
            //如果说泛型是数据类型的泛化，那么委托就是事件方法的泛化
            
            Person[] plist = 
            {
                new Person() { Id = 1, Name = "Lilei" },
                new Person() { Id = 22, Name = "Himing" },
                new Person() { Id = 13, Name = "Kim" },
                new Person() { Id = 674, Name = "Jim" }
             };
            PersonList.Sort<Person>(plist, PersonList.CompareId);
            foreach (Person item in plist)
            {
                Console.WriteLine("{0} {1}",item.Id,item.Name);
            }
        }
        static void Sample04()
        {
            //委托可以通过+ -来添加某些事件
            del dl = Program.Say01;
            dl += Program.Say02;
            dl += Program.Say03;
            dl();
            
        }
        static void Sample05()
        {
            //lmdba的写法：就是为了简便，其他没什么看出什么意义
            Func<string, int, string> dd = (param, param2) =>
                {
                    return param + param2;
                };
            Console.WriteLine(dd("我的学号是", 123));

        }
        static void Say01()
        {
            Console.WriteLine("你好");
        }
        static void Say02()
        {
            Console.WriteLine("我是谁");
        }
        static void Say03()
        {
            Console.WriteLine("你干什么呢");
        }
    }
    public class Operator
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Z { get; set; }
        public int SetValues(int x, int y, int z)
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
            return this.X + this.Y + this.Z;
        }
        public int SetValues(int x)
        {
            this.X = x;
            return this.X;
        }

    }
}
